<template>
  <div class="roles">
    <el-row>
      <el-col :span="24">
        <div class="grid-content bg-purple-dark">
          <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item>权限管理</el-breadcrumb-item>
          <el-breadcrumb-item>角色列表</el-breadcrumb-item>
        </el-breadcrumb>
        </div>
      </el-col>
    </el-row>
    <el-row>
      <el-button type="primary" plain>添加角色</el-button>
    </el-row>
    <el-table
      border
      class="mt-15"
      :data="roleList"
      style="width: 100%">
      <el-table-column type="expand">
        <template slot-scope="scope">
            <el-row v-for="firstChildren in scope.row.children" :key="firstChildren.id">
              <el-col :span="4">
                <el-tag type="primary" closable @close="deleteRight(scope.row,firstChildren.id)">{{firstChildren.authName}}</el-tag>
                <i class="el-icon-arrow-right" v-if="firstChildren.children.length !== 0"></i>
              </el-col>
              <el-col :span="20">
                <el-row v-for="secondChildren in firstChildren.children" :key="secondChildren.id">
                  <el-col :span="4">
                    <el-tag type="success" closable @close="deleteRight(scope.row,secondChildren.id)">{{secondChildren.authName}}</el-tag>
                    <i class="el-icon-arrow-right" v-if="secondChildren.children.length !== 0"></i>
                  </el-col>
                  <el-col :span="20">
                    <el-tag type="warning"
                        closable
                        v-for="thirdChildren in secondChildren.children" :key="thirdChildren.id"
                        @close="deleteRight(scope.row,thirdChildren.id)">
                      {{thirdChildren.authName}}
                      </el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
            <el-row v-if="scope.row.children.length === 0">
              <el-col :span="24">该角色没有分配权限。请前往设置</el-col>
            </el-row>
        </template>
      </el-table-column>
      <el-table-column
        label="角色名称"
        prop="roleName"
        width="165px">
      </el-table-column>
      <el-table-column
        label="描述"
        prop="roleDesc"
        width="120px">
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" type="success" plain icon="el-icon-edit" ></el-button>
          <el-button size="mini" type="danger" plain icon="el-icon-delete" ></el-button>
          <el-button size="mini" type="warning" plain icon="el-icon-check" @click="showRight(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 权限列表对话框 -->
    <el-dialog
      title="权限列表"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose">
      <div class="tree-container">
        <el-tree
          ref="tree"
          :data="rights"
          show-checkbox
          node-key="id"
          :default-expand-all="true"
          :default-checked-keys="selectedRights"
          :props="defaultProps">
        </el-tree>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitGrant">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {getRoleList,deleteRoleRight,getRightList,grantRoleRight} from '@/api'
export default {
  data () {
    return {
      roleList: [],
      dialogVisible: false,
      rights: [],
      defaultProps: {
        children: 'children',
        label: 'authName'
      },
      selectedRights: [], //保存默认选中的权限的id
      currentRole: {}//保存点击的角色
    }
  },
  created() {
    this.init()
  },
  methods: {
    init() {
      getRoleList().then(res => {
        // console.log(res);
        this.roleList = res.data
      })
    },
    //删除权限
    deleteRight(row, rightId) {
      deleteRoleRight({roleId: row.id,rightId: rightId}).then(res => {
        // console.log(res.data)
        // row.children = res.data
        // console.log(res)
        if (res.meta.status === 200) {
          row.children = res.data
        } else {
          this.$message({
            type: 'success',
            message: res.meta.msg
          })
        }
      })
    },
    //展示权限列表
    showRight(row) {
      this.dialogVisible = true
      this.currentRole = row
      getRightList({type: 'tree'}).then(res => {
        // console.log(res)
        this.rights = res.data
      })
      //遍历之前先把数组清空
      this.selectedRights.length = 0
      // 取出当前点击角色的所有权限， 然后遍历到它的第三个children，取出它里面的所有的项的id，让进selectedRights中
      this.currentRole.children.forEach(first => {
        if (first.children && first.children.length != 0) {
          first.children.forEach(second => {
            if (second.children && second.children.length != 0) {
              second.children.forEach(third => {
                this.selectedRights.push(third.id)
              })
            }
          })
        }
      })
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done();
        })
        .catch(_ => {});
    },
    //设置权限并提交
    submitGrant() {
      let rids = this.$refs.tree.getCheckedKeys().join(",")

      grantRoleRight(this.currentRole.id,{rids: rids}).then(res => {
        if (res.meta.status === 200) {
          this.$message({
            type: 'success',
            message: '权限设置成功'
          })
          this.dialogVisible = false
          this.init()
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.roles {
  .el-tag {
    margin-right: 5px;
    margin-bottom: 5px;
  }
  .tree-container {
    height: 300px;
    overflow: auto;
  }
}
</style>
